package com.base.look.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.base.commonlib.utils.RxBus2;
import com.base.look.R;
import com.base.look.bean.LocationCheckedEntity;
import com.base.look.bean.LocationInfo;

import java.util.List;

/**
 * Created by zw on 2020/2/27.
 */

public class SearchLocationRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_HEADER = 0; //Header
    public static final int TYPE_FOOTER = 1; //Footer
    public static final int TYPE_NORMAL = 2; //不带header和footer

    private List<LocationInfo> mData;
    private View mHeaderView;
    private View mFooterView;
    private Activity activity;
    private Context mContext;

    public SearchLocationRecyclerAdapter(Activity activity,Context context, List<LocationInfo> mData){
        this.mData = mData;
        this.activity = activity;
        this.mContext=context;
    }

    public View getHeaderView() {
        return mHeaderView;
    }
    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }
    public View getFooterView() {
        return mFooterView;
    }
    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null){
            return TYPE_NORMAL;
        }
        if (position == 0){
            //第一个item应该加载Header
            return TYPE_HEADER;
        }
        if (position == getItemCount()-1){
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER) {
            return new ListHolder(mHeaderView);
        }
        if(mFooterView != null && viewType == TYPE_FOOTER){
            return new ListHolder(mFooterView);
        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_location_result, parent, false);
        return new ListHolder(layout);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(getItemViewType(position) == TYPE_NORMAL){
            if(holder instanceof ListHolder) {
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                ((ListHolder) holder).name.setText(mData.get(position-1).getName());
                ((ListHolder) holder).address.setText(mData.get(position-1).getAddress2()+"");
                ((ListHolder) holder).relativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LocationCheckedEntity locationCheckedEntity=new LocationCheckedEntity();
                        locationCheckedEntity.setAddress(mData.get(position-1).getName());
                        locationCheckedEntity.setLatitude(mData.get(position-1).getLatitude());
                        locationCheckedEntity.setLonTitude(mData.get(position-1).getLonTitude());
                        locationCheckedEntity.setState(0x00000101);
                        RxBus2.getInstance().post(locationCheckedEntity);
                        activity.finish();
                    }
                });
                return;
            }
            return;
        }else if(getItemViewType(position) == TYPE_HEADER){
            return;
        }else{
            return;
        }
    }


    class ListHolder extends RecyclerView.ViewHolder{
        TextView name;
        TextView address;
        RelativeLayout relativeLayout;
        public ListHolder(View itemView) {
            super(itemView);
            //如果是headerview或者是footerview,直接返回
            if (itemView == mHeaderView){
                return;
            }
            if (itemView == mFooterView){
                return;
            }
            name = itemView.findViewById(R.id.name);
            address = itemView.findViewById(R.id.address);
            relativeLayout =itemView.findViewById(R.id.rel);
        }
    }

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        if(mHeaderView == null && mFooterView == null){
            return mData.size();
        }else if(mHeaderView == null && mFooterView != null){
            return mData.size() + 1;
        }else if (mHeaderView != null && mFooterView == null){
            return mData.size() + 1;
        }else {
            return mData.size() + 2;
        }
    }
}
